package org.zsk.cs.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.zsk.cs.domain.po.Role;
import org.zsk.cs.domain.po.User;
import org.zsk.cs.domain.vo.RoleVo;
import org.zsk.cs.domain.vo.UserVo;
import org.zsk.cs.response.ResultBody;
import org.zsk.cs.service.RoleService;
import org.zsk.fastdev.assign.Assign;

import java.util.List;

/**
 * <p>
 * 角色 controller
 * </p>
 *
 * @author zsk
 * @version 1.0
 * Created on 2020/03/29
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    private final RoleService roleService;
    private final Assign assign;

    @Autowired
    public RoleController(RoleService roleService, Assign assign) {
        this.roleService = roleService;
        this.assign = assign;
    }

    @PostMapping("/a")
    public ResultBody add(@RequestBody RoleVo roleVo) throws Exception {
        Role role=new Role();
        assign.assign(roleVo,role);
        roleService.save(role);
        return ResultBody.success();
    }

    @PutMapping("/u")
    public ResultBody update(@RequestBody RoleVo roleVo) throws Exception {
        Role role=new Role();
        assign.assign(roleVo,role);
        roleService.updateById(role);
        return ResultBody.success();
    }

    @DeleteMapping("/d/{ids}")
    public ResultBody delete(@PathVariable List<Integer> ids) throws Exception {
        roleService.removeByIds(ids);
        return ResultBody.success();
    }

    @GetMapping("/g")
    public ResultBody page(RoleVo roleVo) throws Exception {
        Page<Role> page=new Page<>();
        assign.assign(roleVo,page,"$current,$size");
        return ResultBody.success(roleService.page(page));
    }

    @GetMapping("/search")
    public ResultBody search(RoleVo roleVo) throws Exception {
        return ResultBody.success(roleService.search(roleVo));
    }

    @PostMapping("/addmrb")
    public ResultBody addMenuForRoleBatch(
            @RequestParam List<Integer> rList
            ,@RequestParam List<Integer> mList) throws Exception {
        return ResultBody.success(roleService.addMenuForRoleBatch(rList,mList));
    }

    @PutMapping("/updatemrb")
    public ResultBody updateMenuForRoleBatch(
            @RequestParam List<Integer> rList
            ,@RequestParam List<Integer> mList) throws Exception {
        return ResultBody.success(roleService.updateMenuForRoleBatch(rList,mList));
    }

    @PostMapping("/addrub")
    public ResultBody addUserForRoleBatch(
            @RequestParam List<Long> uList
            ,@RequestParam List<Integer> rList) throws Exception {
        return ResultBody.success(roleService.addUserForRoleBatch(uList,rList));
    }

    @PutMapping("/updaterub")
    public ResultBody updateUserForRoleBatch(
            @RequestParam List<Long> uList
            ,@RequestParam List<Integer> rList) throws Exception {
        return ResultBody.success(roleService.updateUserForRoleBatch(uList,rList));
    }

    @GetMapping("/gmi")
    public ResultBody getMenuIds(Integer roleId) throws Exception {
        return ResultBody.success(roleService.getMenuIdByRoleId(roleId));
    }

    @GetMapping("/gui")
    public ResultBody getUserIds(Integer roleId) throws Exception {
        return ResultBody.success(roleService.getUserIdByRoleId(roleId));
    }
}
